<cfsetting enablecfoutputonly="Yes">

<!--- 
  /**
   * @author  rens@joxx.nl
   * @version 1.0 - 2003.07.25
   */
--->

<cfscript>
  function translate()
  {
    var _s_ERRORHEADER = "[<b>Error</b>]<br>&nbsp;&nbsp;[/@shared/@specific/@udf/translate.cfm]: ";
    
    /*
    * DBUG
    *
    * We will store the time in the first element of the array, the cached bit 
    * in the second one.
    */
    var _lStart = getTickCount();
    if( not isDefined( "request.time.xTranslate" ))
    {
      request.time.xTranslate = arrayNew( 2 );
    }

    if( structCount( arguments ) neq 2 )
    {
      return( _s_ERRORHEADER & "wrong amount of parameters" );
    }

    local.sSearchQuery = "/page/language[@id='#arguments[1]#']/label[@id='#arguments[2]#']";
    
    /*
    * We will have some sort of 'cache' for every request that will be sent to 
    * the webserver. If it's not defined (first translate call), make one, 
    * otherwise: try to find the translation that is store in the cache
    */
    if( not isDefined( "request.translatecache" ))
    {
      request.translatecache = structNew();
    }
    else if( structKeyExists( request.translatecache, arguments[2] ))
    {
      // DBUG
      local.nNewArrayPos = arrayLen( request.time.xTranslate ) + 1;
      request.time.xTranslate[local.nNewArrayPos][1] = getTickCount() - _lStart;
      request.time.xTranslate[local.nNewArrayPos][2] = 1;

      return( structFind( request.translatecache, arguments[2] ));
    }
    
    /*
    * First: we're trying to find this in the LOCAL XML file that has to do 
    * with the translating-things
    */
    if( isDefined( "request.lan.xXMLFile" ))
    {
      local.xElement = XMLSearch( request.lan.xXMLFile, local.sSearchQuery );
      if( arrayLen( local.xElement ) gt 0 )
      {
        // DBUG
        local.nNewArrayPos = arrayLen( request.time.xTranslate ) + 1;
        request.time.xTranslate[local.nNewArrayPos][1] = getTickCount() - _lStart;
        request.time.xTranslate[local.nNewArrayPos][2] = 0;
  
        /* store this translation in cache, then return */
        structInsert( request.translatecache, arguments[2], local.xElement[1].xmltext, true );
        return( local.xElement[1].xmltext );
      }
    }
    
    /*
    * We obviously didn't find the translation we were looking for (otherwise
    * we would have gotten out of here by the return statement. Let's try to 
    * find the translation in the 'uber-file' (globals) ..
    */
    local.xElement = XMLSearch( request.lan.xXMLGlobal, local.sSearchQuery );
    if( arrayLen( local.xElement ) gt 0 )
    {
      // DBUG
      local.nNewArrayPos = arrayLen( request.time.xTranslate ) + 1;
      request.time.xTranslate[local.nNewArrayPos][1] = getTickCount() - _lStart;
      request.time.xTranslate[local.nNewArrayPos][2] = 0;

      /* store this translation in cache, then return */
      structInsert( request.translatecache, arguments[2], local.xElement[1].xmltext, true );
      return( local.xElement[1].xmltext );
    }
    
    return( _s_ERRORHEADER & "no XML elements found for [#local.sSearchQuery#]" );
  }

  /* Copy to a scope where it's visible from everywhere */
  request.udf.translate = translate;
</cfscript>

<cfsetting enablecfoutputonly="No">